package com.bba.manager.user.service.impl;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.bba.manager.common.util.ResponseEnum;
import com.bba.manager.common.model.user.Employee;
import com.bba.manager.user.mapper.EmployeeMapper;
import com.bba.manager.user.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.stereotype.Service;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SYANDZZY
 * @since 2025-08-11
 */
@Service  // 标识为Spring服务层组件
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {


    @Override
    public void register(Employee employee) {
        LambdaQueryChainWrapper<Employee> query = new LambdaQueryChainWrapper<>(baseMapper);
        Employee one = query.eq(Employee::getIdCard, employee.getIdCard()).one();
        if (one != null) {
            throw new RuntimeException(ResponseEnum.USER_HAS_EXISTED.getMessage());
        }
        // 使用BCrypt算法对用户密码进行加密处理
        // userInfo.getPassword() 获取用户输入的原始密码
        // BCrypt.gensalt() 生成一个随机的盐值(salt)，用于增强密码加密的安全性
        // BCrypt.hashpw() 方法将原始密码和盐值结合，生成加密后的密码字符串
        String password=BCrypt.hashpw( employee.getPassword(), BCrypt.gensalt());
        employee.setPassword(password);
        // 保存用户
        this.save(employee);
    }


}
